package com.ef.web.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.ef.constant.ConfirmMessage;
import com.ef.constant.ECallStatus;
import com.ef.constant.ELeadsEventType;
import com.ef.constant.EUserTodoObject;
import com.ef.constant.EUserTodoStatus;
import com.ef.constant.EUserTodoType;
import com.ef.domain.Company;
import com.ef.domain.CorpEventHistory;
import com.ef.domain.User;
import com.ef.domain.UserTodoEvent;
import com.ef.service.CorpEventHistoryService;
import com.ef.service.TodoEventService;
import com.ef.util.CalendarUtil;
import com.ef.util.CustomExample;

public class CorpCCAction extends CompanyAction {

	private static final long serialVersionUID = 1L;

	private CorpEventHistoryService corpEventHistoryService;

	private TodoEventService todoEventService;

	private List<CorpEventHistory> calls;

	private Long currentCallId;

	private String currentCallTelephone;

	private Date currentCallDealTime;

	private ECallStatus currentCallStatus;

	private String currentCallComments;

	private Long todoEventId;

	private EUserTodoType todoEventType;

	private String todoEventComments;

	private Date todoEventDate;

	private String todoEventTime;

	@SuppressWarnings("serial")
	public String list() {
		this.listResult = companyService.findByExample(
				new CustomExample<Company>(this.getModel()) {
					public void appendCondition(Criteria criteria) {
						User user = getSessionUser();
						if (user.getRole() != null
								&& user.getRole().getName().equalsIgnoreCase("CC")) {
							criteria.createCriteria("cc").add(
									Restrictions.eq("id", getSessionUser().getId()));
						}
						this.enableLike(MatchMode.ANYWHERE);
					}
				}, this.getStartIndex(), this.getPageSize());
		return LIST;
	};

	public String save() {
		Company company = this.getModel();
		if (company.getId()==null){
			company.setCc(getSessionUser());
			company.setCcAssignTime(new Date());
		}
		company.setUpdateTime(new Date());
		company.setUpdateUser(getSessionUser());
		companyService.saveOrUpdate(company);
 		this.setResult(ConfirmMessage.COMPANY);
		return EDIT;
	}
	private static SimpleDateFormat timeDf = new SimpleDateFormat("K:mm a",new Locale("en"));
	public String saveCall() {
		Company company = this.saveCompany();
		if (company == null) {
			this.setResult(ConfirmMessage.MSG_SAVELEADS_ERROR);
			calls = new ArrayList<CorpEventHistory>();
			return CALL;
		}
		if (currentCallTelephone != null
				&& currentCallTelephone.trim().length() > 0) {
			CorpEventHistory currentCall = null;
			if (currentCallId == null) {
				currentCall = new CorpEventHistory();
			} else {
				currentCall = corpEventHistoryService.findById(currentCallId);
			}
			currentCall.setComments(currentCallComments);
			currentCall.setCallStatus(currentCallStatus);
			currentCall.setDealTime(new Date());
			currentCall.setEventType(ELeadsEventType.CCCall);
			currentCall.setCompany(company);
			currentCall.setTelephone(currentCallTelephone);
			currentCall.setOperater(getSessionUser());
			corpEventHistoryService.saveOrUpdate(currentCall);
			currentCallId=currentCall.getId();
			company.setLastEventTime(new Date());
			company.setLastEventCommons(currentCallComments);
			companyService.saveOrUpdate(company);
		}
		if (todoEventDate != null) { // todoEventDate 不为空处理
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			String day = df.format(todoEventDate);
			String title = "";
			UserTodoEvent todoEvent = null;
			if (todoEventId == null) {
				todoEvent = new UserTodoEvent();
			} else {
				todoEvent = todoEventService.findById(todoEventId);
			}
			if (todoEventType != null
					&& todoEventType.equals(EUserTodoType.Call)) {
				todoEvent.setGoAction("/clt/CorpCC/call?id=" + company.getId());
				title = "NOTE:" + day + " " + todoEventTime + " need a  "
						+ todoEventType.toString() + " TO " + company.getName();
			} else if (todoEventType != null
					&& todoEventType.equals(EUserTodoType.Demo)) {
				todoEvent.setGoAction("/clt/CorpCC/edit?id=" + company.getId());
				title = "NOTE:" + day + " " + todoEventTime + " need a  "
						+ todoEventType.toString() + " TO " + company.getName();
			}
			todoEvent.setCreateTime(new Date());
			todoEvent.setTodoType(todoEventType);
			todoEvent.setCreateUser(getSessionUser());
			Date time=null;
			if (todoEventTime != null
					&& todoEventTime.trim().length() > 0) {
				try {
					time = timeDf.parse(todoEventTime);
				} catch (ParseException e) {
					System.out.println("TodoEventAction parse time error");
				}
    		}else{
    			try {
    				time = timeDf.parse("9:00 AM");
				} catch (ParseException e) {
					System.out.println("TodoEventAction parse time error");
				}
    		}
			todoEvent.setReminderEndTime(CalendarUtil
					.getDateTime(todoEventDate,time));
			time.setTime(time.getTime()-30*60*1000);
			todoEvent.setReminderStartTime(CalendarUtil
					.getDateTime(todoEventDate,time));
			todoEvent.setToObject(EUserTodoObject.MySelf);
			todoEvent.setStatus(EUserTodoStatus.Valid);
			todoEvent.setToUser(getSessionUser());
			todoEvent.setDetail(todoEventComments);
			todoEvent.setTitle(title);
			todoEventService.saveOrUpdate(todoEvent);
			todoEventId=todoEvent.getId();
		}
		calls = corpEventHistoryService.findByCompany(company);
		this.setResult(ConfirmMessage.Call);
		return CALL;
	}

	public String call() {
		calls = corpEventHistoryService.findByCompany(this.getModel());
		return CALL;
	}

	public List<CorpEventHistory> getCalls() {
		return calls;
	}

	public void setCalls(List<CorpEventHistory> calls) {
		this.calls = calls;
	}

	public TodoEventService getTodoEventService() {
		return todoEventService;
	}

	public void setTodoEventService(TodoEventService todoEventService) {
		this.todoEventService = todoEventService;
	}

	public Long getCurrentCallId() {
		return currentCallId;
	}

	public void setCurrentCallId(Long currentCallId) {
		this.currentCallId = currentCallId;
	}

	public String getCurrentCallTelephone() {
		return currentCallTelephone;
	}

	public void setCurrentCallTelephone(String currentCallTelephone) {
		this.currentCallTelephone = currentCallTelephone;
	}

	public ECallStatus getCurrentCallStatus() {
		return currentCallStatus;
	}

	public void setCurrentCallStatus(ECallStatus currentCallStatus) {
		this.currentCallStatus = currentCallStatus;
	}

	public Long getTodoEventId() {
		return todoEventId;
	}

	public void setTodoEventId(Long todoEventId) {
		this.todoEventId = todoEventId;
	}

	public EUserTodoType getTodoEventType() {
		return todoEventType;
	}

	public void setTodoEventType(EUserTodoType todoEventType) {
		this.todoEventType = todoEventType;
	}

	public String getTodoEventComments() {
		return todoEventComments;
	}

	public void setTodoEventComments(String todoEventComments) {
		this.todoEventComments = todoEventComments;
	}

	public Date getTodoEventDate() {
		return todoEventDate;
	}

	public void setTodoEventDate(Date todoEventDate) {
		this.todoEventDate = todoEventDate;
	}

	public String getCurrentCallComments() {
		return currentCallComments;
	}

	public void setCurrentCallComments(String currentCallComments) {
		this.currentCallComments = currentCallComments;
	}

	public Date getCurrentCallDealTime() {
		return currentCallDealTime;
	}

	public void setCurrentCallDealTime(Date currentCallDealTime) {
		this.currentCallDealTime = currentCallDealTime;
	}

	public String getTodoEventTime() {
		return todoEventTime;
	}

	public void setTodoEventTime(String todoEventTime) {
		this.todoEventTime = todoEventTime;
	}

	public CorpEventHistoryService getCorpEventHistoryService() {
		return corpEventHistoryService;
	}

	public void setCorpEventHistoryService(
			CorpEventHistoryService corpEventHistoryService) {
		this.corpEventHistoryService = corpEventHistoryService;
	}
}
